package com.shen.llmtool.common.utils;

import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Http请求客户端
 */
public class OkHttpUtil {

    /**
     * 创建静态的Logger实例
     */
    private static final Logger log = LoggerFactory.getLogger(OkHttpUtil.class);
    private static final String MEDIA_TYPE_JSON = "application/json; charset=utf-8";

    private static final Object OBJECT = new Object();

    private static OkHttpClient okHttpClient;

    private OkHttpUtil() {

    }

    /**
     * 单例模式获取okhttp客户端
     *
     * @return http客户端实例
     */
    public static OkHttpClient getOkHttpClient() {
        if (okHttpClient == null) {
            synchronized (OBJECT) {
                okHttpClient = getOkHttpClient(600, 600, 600);
            }
        }
        return okHttpClient;
    }

    /**
     * 生成okhttp客户端
     *
     * @return http客户端实例
     */
    public static OkHttpClient getOkHttpClient(int connectTimeOut, int readTimeOut, int writeTimeOut) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(connectTimeOut, TimeUnit.SECONDS);
        builder.readTimeout(readTimeOut, TimeUnit.SECONDS);
        builder.writeTimeout(writeTimeOut, TimeUnit.SECONDS);
        return builder.build();
    }

    /**
     * get请求
     *
     * @param okHttpClient okhttp客户端
     * @param url          请求url
     * @param headers      请求头
     * @return 响应结果
     */
    public static String get(OkHttpClient okHttpClient, String url, Headers headers) {
        Request request = new Request.Builder().url(url).headers(headers).get().build();
        String responseData = request(okHttpClient, url, request);
        log.info("okHttpClient get url:{}, request responseData:{}", url, responseData);
        return responseData;
    }

    /**
     * get请求
     *
     * @param okHttpClient okhttp客户端
     * @param url          请求url
     * @return 响应结果
     */
    public static String get(OkHttpClient okHttpClient, String url) {
        Headers headers = new Headers.Builder().build();
        return get(okHttpClient, url, headers);
    }

    /**
     * get请求
     *
     * @param url 请求url
     * @return 响应结果
     */
    public static String get(String url) {
        OkHttpClient okHttpClient = getOkHttpClient();
        Headers headers = new Headers.Builder().build();
        return get(okHttpClient, url, headers);
    }

    /**
     * post请求
     *
     * @param okHttpClient okhttp客户端
     * @param url          请求url
     * @param bodyJson     请求json
     * @param headers      请求头
     * @return 响应结果
     */
    public static String post(OkHttpClient okHttpClient, String url, String bodyJson, Headers headers) {
        MediaType mediaType = MediaType.get(MEDIA_TYPE_JSON);
        RequestBody responseBody = RequestBody.create(bodyJson, mediaType);
        Request request = new Request.Builder().url(url).headers(headers).post(responseBody).build();
        String responseData = request(okHttpClient, url, request);
        log.info("okHttpClient post url:{}, request responseData:{}", url, responseData);
        return responseData;
    }

    /**
     * post请求
     *
     * @param url      请求url
     * @param bodyJson 请求json
     * @return 响应结果
     */
    public static String post(Map<String, String> headersMap, String url, String bodyJson) {
        OkHttpClient okHttpClient = getOkHttpClient();
        Headers headers = Headers.of(headersMap);
        return post(okHttpClient, url, bodyJson, headers);
    }

    /**
     * post请求
     *
     * @param url      请求url
     * @param bodyJson 请求json
     * @return 响应结果
     */
    public static String post(String url, String bodyJson) {
        OkHttpClient okHttpClient = getOkHttpClient();
        Headers headers = new Headers.Builder().build();
        return post(okHttpClient, url, bodyJson, headers);
    }


    /**
     * 获取响应结果
     *
     * @param okHttpClient http客户端
     * @param url          请求url
     * @param request      请求体
     * @return 响应结果
     */
    public static String request(OkHttpClient okHttpClient, String url, Request request) {
        try (Response response = okHttpClient.newCall(request).execute()) {
            if (response.body() != null) {
                return response.body().string();
            }
        } catch (Exception e) {
            log.error("okHttpClient request error, url:{}, error: ", url, e);
        }
        return "";
    }
}
